require 'rake'
require_relative 'assets_tasks'

namespace :extjs do
  EXTJS_URL           = "http://cdn.sencha.com/ext-4.1.1a-gpl.zip"
  EXTJS_CSS_URL       = "http://cdn.sencha.io/ext-4.1.1a-gpl/resources/css/ext-all.css"
  EXTJS_JS_URL        = "http://cdn.sencha.io/ext-4.1.1a-gpl/ext-all.js"
  EXTJS_JS_DEBUG_URL  = "http://cdn.sencha.io/ext-4.1.1a-gpl/ext-all-debug.js"

  desc "Install Mini GPL ExtJS 4.1.1 in assets/ directory"
  task :install => [:create_assets, :download_mini ]
  desc "Install Full GPL ExtJS 4.1.1 in assets/ directory"
  task :install_full => [:create_assets, :download, :unzip, :clean]
  task :create_assets do
    Rake::Task["assets:create"].invoke
  end
  task :download_mini do
    mkdir_p "assets/ext-4.1.1a"
    sh "wget #{EXTJS_CSS_URL} -N -P assets/ext-4.1.1a/ -q"
    sh "wget #{EXTJS_JS_URL} -N -P assets/ext-4.1.1a/ -q"
    sh "wget #{EXTJS_JS_DEBUG_URL} -N -P assets/ext-4.1.1a/ -q"
  end
  task :download do
    sh "wget #{EXTJS_URL} -N -O assets/extjs.zip -q"
  end
  task :unzip, :target_file do
    sh "unzip -q -o assets/extjs.zip -d assets/"
  end
  desc "Remove ExtJS 4.1.1 from assets/ directroy"
  task :remove do
    if File.exist? "assets/ext-4.1.1a/"
      rm_rf "assets/ext-4.1.1a"
      Rake::Task["assets:clean"].invoke
    else
      abort "It seems that ExtJS is not installed"
    end
  end
  task :clean do
    rm_rf "assets/extjs.zip"
  end
end
